home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / win / tkWinWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-14  |  12.1 KB  |  540 lines

  1. /* 
  2.  * tkWinWindow.c --
  3.  *
  4.  *    Xlib emulation routines for Windows related to creating,
  5.  *    displaying and destroying windows.
  6.  *
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tkWinWindow.c 1.13 96/04/11 17:52:20
  13.  */
  14.  
  15. #include "tkWinInt.h"
  16.  
  17. /*
  18.  * Forward declarations for procedures defined in this file:
  19.  */
  20.  
  21. static void        NotifyVisibility _ANSI_ARGS_((XEvent *eventPtr,
  22.                 TkWindow *winPtr));
  23. static void         StackWindow _ANSI_ARGS_((Window w, Window sibling,
  24.                 int stack_mode));
  25.  
  26. /*
  27.  *----------------------------------------------------------------------
  28.  *
  29.  * TkMakeWindow --
  30.  *
  31.  *    Creates a Windows window object based on the current attributes
  32.  *    of the specified TkWindow.
  33.  *
  34.  * Results:
  35.  *    Returns a pointer to a new TkWinDrawable cast to a Window.
  36.  *
  37.  * Side effects:
  38.  *    Creates a new window.
  39.  *
  40.  *----------------------------------------------------------------------
  41.  */
  42.  
  43. Window
  44. TkMakeWindow(winPtr, parent)
  45.     TkWindow *winPtr;
  46.     Window parent;
  47. {
  48.     HWND parentWin;
  49.     TkWinDrawable *twdPtr;
  50.     int style;
  51.     
  52.  
  53.     twdPtr = (TkWinDrawable*) ckalloc(sizeof(TkWinDrawable));
  54.     if (twdPtr == NULL) {
  55.     return None;
  56.     }
  57.  
  58.     twdPtr->type = TWD_WINDOW;
  59.     twdPtr->window.winPtr = winPtr;
  60.  
  61.     if (parent != None) {
  62.     parentWin = TkWinGetHWND(parent);
  63.     style = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  64.     } else {
  65.     parentWin = NULL;
  66.     style = WS_POPUP | WS_CLIPCHILDREN;
  67.     }
  68.  
  69.     twdPtr->window.handle = CreateWindow(TK_WIN_CHILD_CLASS_NAME, "", style,
  70.         winPtr->changes.x, winPtr->changes.y,
  71.         winPtr->changes.width, winPtr->changes.height,
  72.         parentWin, NULL, TkWinGetAppInstance(), twdPtr);
  73.  
  74.     if (twdPtr->window.handle == NULL) {
  75.     ckfree((char *) twdPtr);
  76.     twdPtr = NULL;
  77.     }
  78.  
  79.     return (Window)twdPtr;
  80. }
  81.  
  82. /*
  83.  *----------------------------------------------------------------------
  84.  *
  85.  * XDestroyWindow --
  86.  *
  87.  *    Destroys the given window.
  88.  *
  89.  * Results:
  90.  *    None.
  91.  *
  92.  * Side effects:
  93.  *    Sends the WM_DESTROY message to the window and then destroys
  94.  *    it the Win32 resources associated with the window.
  95.  *
  96.  *----------------------------------------------------------------------
  97.  */
  98.  
  99. void
  100. XDestroyWindow(display, w)
  101.     Display* display;
  102.     Window w;
  103. {
  104.     TkWinDrawable *twdPtr = (TkWinDrawable *)w;
  105.     TkWindow *winPtr = TkWinGetWinPtr(w);
  106.     HWND hwnd = TkWinGetHWND(w);
  107.  
  108.     display->request++;
  109.  
  110.     /*
  111.      * Remove references to the window in the pointer module, and 
  112.      * then remove the backpointer from the drawable.
  113.      */
  114.  
  115.     TkWinPointerDeadWindow(winPtr);
  116.     twdPtr->window.winPtr = NULL;
  117.  
  118.     /*
  119.      * Don't bother destroying the window if we are going to destroy
  120.      * the parent later.  Also if the window has already been destroyed
  121.      * then we need to free the drawable now.
  122.      */
  123.  
  124.     if (!hwnd) {
  125.     ckfree((char *)twdPtr);
  126.     } else if (!(winPtr->flags & TK_PARENT_DESTROYED)) {
  127.     DestroyWindow(hwnd);
  128.     }
  129. }
  130.  
  131. /*
  132.  *----------------------------------------------------------------------
  133.  *
  134.  * XMapWindow --
  135.  *
  136.  *    Cause the given window to become visible.
  137.  *
  138.  * Results:
  139.  *    None
  140.  *
  141.  * Side effects:
  142.  *    Causes the window state to change, and generates a MapNotify
  143.  *    event.
  144.  *
  145.  *----------------------------------------------------------------------
  146.  */
  147.  
  148. void
  149. XMapWindow(display, w)
  150.     Display* display;
  151.     Window w;
  152. {
  153.     XEvent event;
  154.     TkWindow *parentPtr;
  155.     TkWindow *winPtr = TkWinGetWinPtr(w);
  156.  
  157.     display->request++;
  158.  
  159.     ShowWindow(TkWinGetHWND(w), SW_SHOWNORMAL);
  160.     winPtr->flags |= TK_MAPPED;
  161.  
  162.     event.type = MapNotify;
  163.     event.xmap.serial = display->request;
  164.     event.xmap.send_event = False;
  165.     event.xmap.display = display;
  166.     event.xmap.event = winPtr->window;
  167.     event.xmap.window = winPtr->window;
  168.     event.xmap.override_redirect = winPtr->atts.override_redirect;
  169.     Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
  170.  
  171.     /*
  172.      * Check to see if this window is visible now.  If all of the parent
  173.      * windows up to the first toplevel are mapped, then this window and
  174.      * its mapped children have just become visible.
  175.      */
  176.  
  177.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  178.     for (parentPtr = winPtr->parentPtr; ;
  179.             parentPtr = parentPtr->parentPtr) {
  180.         if ((parentPtr == NULL) || !(parentPtr->flags & TK_MAPPED)) {
  181.         return;
  182.         }
  183.         if (parentPtr->flags & TK_TOP_LEVEL) {
  184.         break;
  185.         }
  186.     }
  187.     }
  188.  
  189.     /*
  190.      * Generate VisibilityNotify events for this window and its mapped
  191.      * children.
  192.      */
  193.  
  194.     event.type = VisibilityNotify;
  195.     event.xvisibility.state = VisibilityUnobscured;
  196.     NotifyVisibility(&event, winPtr);
  197. }
  198.  
  199. /*
  200.  *----------------------------------------------------------------------
  201.  *
  202.  * NotifyVisibility --
  203.  *
  204.  *    This function recursively notifies the mapped children of the
  205.  *    specified window of a change in visibility.  Note that we don't
  206.  *    properly report the visibility state, since Windows does not
  207.  *    provide that info.  The eventPtr argument must point to an event
  208.  *    that has been completely initialized except for the window slot.
  209.  *
  210.  * Results:
  211.  *    None.
  212.  *
  213.  * Side effects:
  214.  *    Generates lots of events.
  215.  *
  216.  *----------------------------------------------------------------------
  217.  */
  218.  
  219. static void
  220. NotifyVisibility(eventPtr, winPtr)
  221.     XEvent *eventPtr;        /* Initialized VisibilityNotify event. */
  222.     TkWindow *winPtr;        /* Window to notify. */
  223. {
  224.     eventPtr->xvisibility.window = winPtr->window;
  225.     Tk_QueueWindowEvent(eventPtr, TCL_QUEUE_TAIL);
  226.     for (winPtr = winPtr->childList; winPtr != NULL;
  227.         winPtr = winPtr->nextPtr) {
  228.     if (winPtr->flags & TK_MAPPED) {
  229.         NotifyVisibility(eventPtr, winPtr);
  230.     }
  231.     }
  232. }
  233.  
  234. /*
  235.  *----------------------------------------------------------------------
  236.  *
  237.  * XUnmapWindow --
  238.  *
  239.  *    Cause the given window to become invisible.
  240.  *
  241.  * Results:
  242.  *    None
  243.  *
  244.  * Side effects:
  245.  *    Causes the window state to change, and generates an UnmapNotify
  246.  *    event.
  247.  *
  248.  *----------------------------------------------------------------------
  249.  */
  250.  
  251. void
  252. XUnmapWindow(display, w)
  253.     Display* display;
  254.     Window w;
  255. {
  256.     XEvent event;
  257.     TkWindow *winPtr = TkWinGetWinPtr(w);
  258.  
  259.     display->request++;
  260.  
  261.     ShowWindow(TkWinGetHWND(w), SW_HIDE);
  262.     winPtr->flags &= ~TK_MAPPED;
  263.  
  264.     event.type = UnmapNotify;
  265.     event.xunmap.serial = display->request;
  266.     event.xunmap.send_event = False;
  267.     event.xunmap.display = display;
  268.     event.xunmap.event = winPtr->window;
  269.     event.xunmap.window = winPtr->window;
  270.     event.xunmap.from_configure = False;
  271.     Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
  272. }
  273.  
  274. /*
  275.  *----------------------------------------------------------------------
  276.  *
  277.  * XMoveResizeWindow --
  278.  *
  279.  *    Move and resize a window relative to its parent.
  280.  *
  281.  * Results:
  282.  *    None.
  283.  *
  284.  * Side effects:
  285.  *    Repositions and resizes the specified window.
  286.  *
  287.  *----------------------------------------------------------------------
  288.  */
  289.  
  290. void
  291. XMoveResizeWindow(display, w, x, y, width, height)
  292.     Display* display;
  293.     Window w;
  294.     int x;            /* Position relative to parent. */
  295.     int y;
  296.     unsigned int width;
  297.     unsigned int height;
  298. {
  299.     display->request++;
  300.     MoveWindow(TkWinGetHWND(w), x, y, width, height, TRUE);
  301. }
  302.  
  303. /*
  304.  *----------------------------------------------------------------------
  305.  *
  306.  * XMoveWindow --
  307.  *
  308.  *    Move a window relative to its parent.
  309.  *
  310.  * Results:
  311.  *    None.
  312.  *
  313.  * Side effects:
  314.  *    Repositions the specified window.
  315.  *
  316.  *----------------------------------------------------------------------
  317.  */
  318.  
  319. void
  320. XMoveWindow(display, w, x, y)
  321.     Display* display;
  322.     Window w;
  323.     int x;
  324.     int y;
  325. {
  326.     TkWindow *winPtr = TkWinGetWinPtr(w);
  327.  
  328.     display->request++;
  329.  
  330.     MoveWindow(TkWinGetHWND(w), x, y, winPtr->changes.width,
  331.         winPtr->changes.height, TRUE);
  332. }
  333.  
  334. /*
  335.  *----------------------------------------------------------------------
  336.  *
  337.  * XResizeWindow --
  338.  *
  339.  *    Resize a window.
  340.  *
  341.  * Results:
  342.  *    None.
  343.  *
  344.  * Side effects:
  345.  *    Resizes the specified window.
  346.  *
  347.  *----------------------------------------------------------------------
  348.  */
  349.  
  350. void
  351. XResizeWindow(display, w, width, height)
  352.     Display* display;
  353.     Window w;
  354.     unsigned int width;
  355.     unsigned int height;
  356. {
  357.     TkWindow *winPtr = TkWinGetWinPtr(w);
  358.  
  359.     display->request++;
  360.  
  361.     MoveWindow(TkWinGetHWND(w), winPtr->changes.x, winPtr->changes.y, width,
  362.         height, TRUE);
  363. }
  364.  
  365. /*
  366.  *----------------------------------------------------------------------
  367.  *
  368.  * XRaiseWindow --
  369.  *
  370.  *    Change the stacking order of a window.
  371.  *
  372.  * Results:
  373.  *    None.
  374.  *
  375.  * Side effects:
  376.  *    Changes the stacking order of the specified window.
  377.  *
  378.  *----------------------------------------------------------------------
  379.  */
  380.  
  381. void
  382. XRaiseWindow(display, w)
  383.     Display* display;
  384.     Window w;
  385. {
  386.     HWND window = TkWinGetHWND(w);
  387.  
  388.     display->request++;
  389.     SetWindowPos(window, HWND_TOPMOST, 0, 0, 0, 0,
  390.         SWP_NOMOVE | SWP_NOSIZE);
  391. }
  392.  
  393. /*
  394.  *----------------------------------------------------------------------
  395.  *
  396.  * XConfigureWindow --
  397.  *
  398.  *    Change the size, position, stacking, or border of the specified
  399.  *    window.
  400.  *
  401.  * Results:
  402.  *    None.
  403.  *
  404.  * Side effects:
  405.  *    Changes the attributes of the specified window.  Note that we
  406.  *    ignore the passed in values and use the values stored in the
  407.  *    TkWindow data structure.
  408.  *
  409.  *----------------------------------------------------------------------
  410.  */
  411.  
  412. void
  413. XConfigureWindow(display, w, value_mask, values)
  414.     Display* display;
  415.     Window w;
  416.     unsigned int value_mask;
  417.     XWindowChanges* values;
  418. {
  419.     TkWindow *winPtr = TkWinGetWinPtr(w);
  420.     HWND window = TkWinGetHWND(w);
  421.     HWND insertAfter;
  422.  
  423.     display->request++;
  424.  
  425.     /*
  426.      * Change the shape and/or position of the window.
  427.      */
  428.  
  429.     if (value_mask & (CWX|CWY|CWWidth|CWHeight)) {
  430.     MoveWindow(window, winPtr->changes.x, winPtr->changes.y,
  431.         winPtr->changes.width, winPtr->changes.height, TRUE);
  432.     }
  433.  
  434.     /*
  435.      * Change the stacking order of the window.
  436.      */
  437.  
  438.     if (value_mask & CWStackMode) {
  439.     if ((value_mask & CWSibling) && (values->sibling != None)) {
  440.         HWND sibling = TkWinGetHWND(values->sibling);
  441.  
  442.         /*
  443.          * Windows doesn't support the Above mode, so we insert the
  444.          * window just below the sibling and then swap them.
  445.          */
  446.  
  447.         if (values->stack_mode == Above) {
  448.         SetWindowPos(window, sibling, 0, 0, 0, 0,
  449.             SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
  450.         insertAfter = window;
  451.         window = sibling;
  452.         } else {
  453.         insertAfter = sibling;
  454.         }
  455.     } else {
  456.         insertAfter = (values->stack_mode == Above) ? HWND_TOP
  457.         : HWND_BOTTOM;
  458.     }
  459.         
  460.     SetWindowPos(window, insertAfter, 0, 0, 0, 0,
  461.         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
  462.     } 
  463. }
  464.  
  465. /*
  466.  *----------------------------------------------------------------------
  467.  *
  468.  * XClearWindow --
  469.  *
  470.  *    Clears the entire window to the current background color.
  471.  *
  472.  * Results:
  473.  *    None.
  474.  *
  475.  * Side effects:
  476.  *    Erases the current contents of the window.
  477.  *
  478.  *----------------------------------------------------------------------
  479.  */
  480.  
  481. void
  482. XClearWindow(display, w)
  483.     Display* display;
  484.     Window w;
  485. {
  486.     RECT rc;
  487.     HBRUSH brush;
  488.     HPALETTE oldPalette, palette;
  489.     TkWindow *winPtr;
  490.     HWND hwnd = TkWinGetHWND(w);
  491.     HDC dc = GetDC(hwnd);
  492.  
  493.     palette = TkWinGetPalette(display->screens[0].cmap);
  494.     oldPalette = SelectPalette(dc, palette, FALSE);
  495.  
  496.     display->request++;
  497.  
  498.     winPtr = TkWinGetWinPtr(w);
  499.     brush = CreateSolidBrush(winPtr->atts.background_pixel);
  500.     GetWindowRect(hwnd, &rc);
  501.     rc.right = rc.right - rc.left;
  502.     rc.bottom = rc.bottom - rc.top;
  503.     rc.left = rc.top = 0;
  504.     FillRect(dc, &rc, brush);
  505.  
  506.     DeleteObject(brush);
  507.     SelectPalette(dc, oldPalette, TRUE);
  508.     ReleaseDC(hwnd, dc);
  509. }
  510.  
  511. /*
  512.  *----------------------------------------------------------------------
  513.  *
  514.  * XChangeWindowAttributes --
  515.  *
  516.  *    This function is called when the attributes on a window are
  517.  *    updated.  Since Tk maintains all of the window state, the only
  518.  *    relevant value is the cursor.
  519.  *
  520.  * Results:
  521.  *    None.
  522.  *
  523.  * Side effects:
  524.  *    May cause the mouse position to be updated.
  525.  *
  526.  *----------------------------------------------------------------------
  527.  */
  528.  
  529. void
  530. XChangeWindowAttributes(display, w, valueMask, attributes)
  531.     Display* display;
  532.     Window w;
  533.     unsigned long valueMask;
  534.     XSetWindowAttributes* attributes;
  535. {
  536.     if (valueMask & CWCursor) {
  537.     XDefineCursor(display, w, attributes->cursor);
  538.     }
  539. }
  540.